//
//  JourneyCoutry_CityDetilViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyCoutry_CityDetilViewController.h"
#import "JourneryNetHandler.h"
#import "JourneyPhotoViewController.h"
#import "DetilWebViewController.h"
#import "BoutiqueTraViewController.h"
#import "HotPlaceDataSource.h"
#import "HotPlaceCollectionViewCell.h"
#import "HotPlaceViewController.h"
#import "MBProgressHUD.h"
static CGFloat ImageHeight  = 200.0;
static CGFloat ImageWidth  = 320.0;
@interface JourneyCoutry_CityDetilViewController () <UIScrollViewDelegate, UIWebViewDelegate, UICollectionViewDataSource,UICollectionViewDelegate,MBProgressHUDDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIScrollView *imgContainer;
@property (nonatomic, retain) UIScrollView *wrapperScroll;
@property (nonatomic , retain)UIImageView *imgProfile;
@property (nonatomic , retain)UILabel *nameLable;
@property (nonatomic , retain)UILabel *recommended_resonLable;
@property (nonatomic , retain)NSMutableArray *resultArr;
@property (nonatomic , retain)UIWebView *webView;

@property (nonatomic , retain)UICollectionView *JCCollectionView;
@property (nonatomic , retain)NSMutableArray *JCCArr;

@property (nonatomic , retain)MBProgressHUD *progress;
@end

@implementation JourneyCoutry_CityDetilViewController

- (void)handlerNet

{
    NSLog(@"3333333");
    NSString *str = [[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/",self.coutry_CityData.type,self.coutry_CityData.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"!!!!!!!%@",str);
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        //        self.resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil ];
        
        self.resultArr = [data objectForKey:@"hottest_places"];
        NSString *str = [[_resultArr objectAtIndex:0] valueForKey:@"photo"];
        [self.imgProfile setImageWithURL:[NSURL URLWithString: [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
        self.nameLable.text = [data objectForKey:@"name"];
        self.recommended_resonLable.text = [data objectForKey:@"recommended_reason"];
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addView];
    
    self.progress = [[MBProgressHUD showHUDAddedTo:self.view animated:YES]retain];
    [_progress show:YES];
    self.progress.delegate = self;
    
    [self hotplaceNethandler];
    
    [self handlerNet];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset   = self.scrollView.contentOffset.y;
    CGFloat xOffset   = self.wrapperScroll.contentOffset.x;
    
    if (yOffset < 0) {
        
        CGFloat pageWidth = self.wrapperScroll.frame.size.width;
        int page = floor((self.wrapperScroll.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        
        CGFloat factor = ((ABS(yOffset)+ ImageHeight )*ImageWidth)/ ImageHeight;
        
        CGRect f = CGRectMake((-(factor-ImageWidth)/2) + 320 *page, 20, ImageWidth, ImageHeight);
        f.size.height = ImageHeight+ABS(yOffset);
        f.size.width = factor;
        _imgProfile.frame = f;
        
        CGRect frame = self.imgContainer.frame;
        frame.origin.y = 0;
        self.imgContainer.frame = frame;
        
        
        
    } else {
        self.imgContainer.contentOffset = CGPointMake(xOffset, yOffset);
    }
    
    
}


- (void)addView
{
    self.imgContainer  = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ImageWidth, self.view.bounds.size.height)];
    self.imgContainer.backgroundColor = [UIColor clearColor];
    self.imgContainer.contentSize = CGSizeMake(ImageWidth, self.view.bounds.size.height);
    
    [self.view addSubview:self.imgContainer];
    self.imgProfile = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _imgContainer.frame.size.width, ImageHeight)];
    [self.imgContainer addSubview:self.imgProfile];
    
    UIView *detilView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height)];
    detilView.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    detilView.layer.cornerRadius = 10;
    detilView.clipsToBounds = YES;
    //    CGRect frame = detilView.frame;
    //    frame.origin.y = ImageHeight;
    //    detilView.frame = frame;
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.contentSize = CGSizeMake(320, detilView.frame.size.height+ImageHeight);
    //    [self.scrollView addSubview:self.wrapperScroll];
    [self.scrollView addSubview:detilView];
    [self.view addSubview:self.scrollView];
    
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, detilView.frame.size.width, 50)];
    //    _nameLable.backgroundColor = [UIColor redColor];
    _nameLable.textAlignment = NSTextAlignmentCenter;
    [detilView addSubview:_nameLable];
    
    
    CGRect backFram =CGRectMake(0, 5, 40, 40);
    UIButton *backBtu = [UIButton buttonWithTitle:@"<" frame:backFram target:self action:@selector(backBtuAction:)];
    backBtu.backgroundColor = [UIColor whiteColor];
    [backBtu setTintColor:[UIColor blackColor]];
    backBtu.layer.cornerRadius = 20;
    backBtu.alpha = 0.5;
    [self.view addSubview:backBtu];
    
    //不可错过
    UIButton *noMissBtu = [UIButton buttonWithType:UIButtonTypeCustom];
    noMissBtu.frame =CGRectMake(20, 80, detilView.frame.size.width / 4, 50);
    
    [noMissBtu setTitle: @"不可错过"forState:UIControlStateNormal];
    [noMissBtu addTarget:self action:@selector(nomissBtu:) forControlEvents:UIControlEventTouchUpInside ];
    noMissBtu.backgroundColor = [UIColor colorFromHexCode:@"#668B8B"];
    noMissBtu.tintColor = [UIColor blackColor];
    noMissBtu.layer.cornerRadius = 10;
    [detilView addSubview:noMissBtu];
    
    //精品游记
    UIButton *boutiqueTravelBtu = [UIButton buttonWithType:UIButtonTypeCustom];
    boutiqueTravelBtu.frame =CGRectMake(detilView.frame.size.width / 4 + 40, 80, detilView.frame.size.width / 4, 50);
    [boutiqueTravelBtu setTitle:@"精品游记" forState:UIControlStateNormal];
    [boutiqueTravelBtu addTarget:self action:@selector(boutiqueTravelBtu:) forControlEvents:UIControlEventTouchUpInside];
    boutiqueTravelBtu.backgroundColor = [UIColor colorFromHexCode:@"#668B8B"];
    [boutiqueTravelBtu setTintColor: [UIColor blackColor]];
    boutiqueTravelBtu.layer.cornerRadius= 10;
    [detilView addSubview:boutiqueTravelBtu];
    //实用须知
    UIButton *appliedNoticeBtu = [UIButton buttonWithType:UIButtonTypeSystem ];
    appliedNoticeBtu.frame =  CGRectMake(detilView.frame.size.width * 2 / 4 + 60, 80, detilView.frame.size.width / 4, 50);
    [appliedNoticeBtu setTitle:@"实用须知" forState:UIControlStateNormal];
    [appliedNoticeBtu addTarget:self action:@selector(appliedNoticeBtu:) forControlEvents:UIControlEventTouchUpInside];
    appliedNoticeBtu.tintColor = [UIColor whiteColor];
    //    [appliedNoticeBtu setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
    appliedNoticeBtu.backgroundColor = [UIColor colorFromHexCode:@"#668B8B"];
    appliedNoticeBtu.layer.cornerRadius = 10;
    [detilView addSubview:appliedNoticeBtu];
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(140, 130);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 15);
    
    self.JCCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 140, detilView.frame.size.width, detilView.frame.size.height - 100) collectionViewLayout:flowLayout];
    _JCCollectionView.backgroundColor = [UIColor whiteColor];
    _JCCollectionView.delegate = self;
    _JCCollectionView.dataSource = self;
    [detilView addSubview:_JCCollectionView];
    
    [_JCCollectionView registerClass:[HotPlaceCollectionViewCell class] forCellWithReuseIdentifier:@"hotplace"];
    
}

#pragma mark hotplaceNethandler
- (void)hotplaceNethandler
{
    NSString *la = [NSString stringWithFormat:@"%@",[self.coutry_CityData.location objectForKey:@"lat"]];
    NSString *lon = [NSString stringWithFormat:@"%@",[self.coutry_CityData.location objectForKey:@"lng"]];
    NSString *str = nil;
    if ([la isEqual:nil] || [lon isEqual:nil]) {
        str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=0",self.coutry_CityData.type,self.coutry_CityData.idNumber];
    }else{
        str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=0&latitude=%@&longitude=%@",self.coutry_CityData.type,self.coutry_CityData.idNumber,la,lon];}
    self.JCCArr = [NSMutableArray array];
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        NSArray *arr = [data objectForKey:@"items"];
        
        for (NSInteger i = 0; i < 6; i++) {
            HotPlaceDataSource *data = [[HotPlaceDataSource alloc]initWithDictionary:arr[i]
                                        ];
            
            [self.JCCArr addObject:data];
            [data release];
        }
        [self.JCCollectionView reloadData];
        
        [_progress hide:YES];
    }];
    
    NSLog(@"%@",str);
}

#pragma mark collectionView deleget
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_JCCArr count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotPlaceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotplace" forIndexPath:indexPath];
    cell.hotData =[self.JCCArr objectAtIndex:indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotPlaceViewController *hotVC = [[HotPlaceViewController alloc]init];
    hotVC.idNumber = self.coutry_CityData.idNumber;
    hotVC.type = self.coutry_CityData.type;
    hotVC.la =[NSString stringWithFormat:@"%@",[self.coutry_CityData.location objectForKey:@"lat"]];
    hotVC.lon =[NSString stringWithFormat:@"%@",[self.coutry_CityData.location objectForKey:@"lng"]];
    
    [self.navigationController pushViewController:hotVC animated:NO];
    
    [hotVC release];
}

#pragma mark backBtu
- (void)backBtuAction: (UIButton *)btu
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 不可错过 精品游记 实用须知
- (void)nomissBtu:(UIButton *)btu
{
    
    NSString *str =[[NSString stringWithFormat: @"http://breadtrip.com/mobile/destination/%@/%@/intro/",self.coutry_CityData.type,self.coutry_CityData.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    DetilWebViewController *webVC = [[DetilWebViewController alloc]init];
    webVC.str = str;
    [self.navigationController pushViewController: webVC animated:YES];
    [webVC release];
    
}

- (void)boutiqueTravelBtu:(UIButton *)btu
{
    BoutiqueTraViewController *bouVC = [[BoutiqueTraViewController alloc]init];
    
    bouVC.type = self.coutry_CityData.type;
    bouVC.idNumber = self.coutry_CityData.idNumber;
    bouVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:bouVC animated:YES];
    
    [bouVC release];
}

- (void)appliedNoticeBtu : (UIButton *)btu
{
    NSString *str =[[NSString stringWithFormat: @"http://breadtrip.com/mobile/destination/%@/%@/overview/",self.coutry_CityData.type,self.coutry_CityData.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    DetilWebViewController *webVC = [[DetilWebViewController alloc]init];
    webVC.str = str;
    [self.navigationController pushViewController: webVC animated:YES];
    [webVC release];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y == 0) {
        JourneyPhotoViewController *jourPhotoVC = [[JourneyPhotoViewController alloc]init];
        jourPhotoVC.idNumber = self.coutry_CityData.idNumber;
        jourPhotoVC.type = self.coutry_CityData.type;
        [self.navigationController pushViewController:jourPhotoVC animated:YES];
        [jourPhotoVC release];
    }
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    CGRect bounds = self.view.frame;
    self.scrollView.frame = bounds;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    
    [_progress removeFromSuperview];
    [_progress release];
    _progress  = nil;
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
